<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    class Permission extends MX_Controller{
        
        protected $_templates;
        protected $_module_name = "";
        
        function __construct() {
            parent::__construct();

            $this->_module_name = $this->router->fetch_module();
            $this->pre_message ="";
            $this->lang->load('access');
            $this->load->model('permission_model','permission');
            //$this->permit_library->check_permit();
            $this->session->set_userdata(array('Url'=>uri_string()));          
        }
        function index(){
            $this->acl->check('view','','',base_url());
            $data['title'] = lang('permission.list');
            $data['page']  = (int)$this->uri->segment('4');
            if($this->acl->check('add'))
                $data['add']= $this->_module_name.'/permission/add';
            $data['_module_name']=$this->_module_name;
            
            $data['list'] = $this->permission->getAllPermission();
            $data['num'] = count($data['list']);
            
            $this->_templates['page'] = 'permission/list';
            $this->site_lib->load($this->_templates['page'],$data);
        }        
        /*
         * @author : Vo Van Hieu
         * @method : Build data permission
         * @date   : 21/06/2013
         * @modify : 
         */
        function buildData($arr_data){
            $dataUpdate['permission_code']=$arr_data['permission_code'];
            $dataUpdate['permission_name']=$arr_data['permission_name'];
            $dataUpdate['permission_comment']=$arr_data['permission_comment'];
            return $dataUpdate;
        }
        /*
         * @author : Vo Van Hieu
         * @method : add new permission
         * @date   : 21/06/2013
         * @modify : 
         */
        function add(){
            $this->acl->check('add','','',base_url());
            
            $data['title'] = lang('permission.add');
            
            if(!empty($_POST['bt_submit'])){
               $this->form_validation->set_rules('permission_code',lang('permission.code'),'trim|required');
               $this->form_validation->set_rules('permission_name',lang('permission.name'),'trim|required'); 
               if($this->form_validation->run() == FALSE){
                  $this->pre_message = validation_errors();
               }else{
                    $dataSave=$this->buildData($this->input->post());
                    if($this->permission->saveData(0,$dataSave)){
                       $this->session->set_flashdata('message',lang('save_success'));
                       redirect($this->_module_name.'/permission');
                    }else{
                       $this->pre_message = lang('save_unsuccess');
                       redirect($this->_module_name.'/permission/add');
                    }
               } 
            }
            
            $data['message'] = $this->pre_message;          
            $this->_templates['page'] = 'permission/add';
            $this->site_lib->load($this->_templates['page'],$data);
        }
          /*
         * @author : Vo Van Hieu
         * @method : edit permission
         * @date   : 21/06/2013
         * @modify : 
         */
        function edit($id){
          $this->acl->check('edit','','',base_url());  
            
          $data['title'] = lang('permission.edit');
          $data['item'] = $this->permission->getPermissionByID($id);
          
          if(!empty($_POST['bt_submit'])){
             $this->form_validation->set_rules('permission_code',lang('permission.code'),'trim|required');
             $this->form_validation->set_rules('permission_name',lang('permission.name'),'trim|required');
             
             if($this->form_validation->run() == FALSE){
                $this->pre_message = validation_errors();
             }else{  
                $dataSave=$this->buildData($this->input->post());
                if($this->permission->saveData($id,$dataSave)){
                   $this->session->set_flashdata('message',lang('save_success'));
                   redirect($this->_module_name.'/permission');
                }else{
                   $this->pre_message = lang('save_unsuccess');
                   redirect($this->_module_name.'/permission/edit/'.$id);
                }
             } 
          }
          $data['message'] = $this->pre_message;          
          $this->_templates['page'] = 'permission/edit';
          $this->site_lib->load($this->_templates['page'],$data);
        }  
        /*
         * @author : Vo Van Hieu
         * @method : delete permission
         * @date   : 21/06/2013
         * @modify : 
         */
        function del($id,$page=0){
            $this->acl->check('del','','',base_url());
            
            if($this->permission->delete($id))
                $this->session->set_flashdata('message',lang('delete_success'));
            else 
                $this->session->set_flashdata('message',lang('delete_unsuccess'));

            redirect($this->_module_name.'/permission/index/'.$page);
        }
        /*
         * @author : Vo Van Hieu
         * @method : deletes permission
         * @date   : 21/06/2013
         * @modify : 
         */
        function dels(){
            $this->acl->check('dels','','',base_url());
            
            if(!empty($_POST['ar_id']))
            {
                $page = $this->input->post('page')?(int)$this->input->post('page'):0;
                $ar_id = $this->input->post('ar_id');

                if(!empty($_POST['btn_submit']))
                {
                    for($i = 0; $i < sizeof($ar_id); $i ++) {
                        if ($ar_id[$i]){
                            if($this->permission->delete($ar_id[$i]))
                                $this->session->set_flashdata('message',lang('delete_success'));
                            else 
                                $this->session->set_flashdata('message',lang('delete_unsuccess'));
                        }
                    }
                }
            }
            redirect($this->_module_name.'/permission/index/'.$page);
        }
    }
/* End of file group.php */
/* Location: ./application/webadmin/groups/system/controllers/group.php */
